package test;

import bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.InputStream;
import java.util.List;

/**
 * @author 飞哥
 * @version V1.0
 * @package test
 * @date 2021/6/26 11:44
 * MyBatis持久层框架的第一个练习
 */
public class MyApp {
    public static void main(String[] args) throws Exception {
        //1.读取mybatis主配置文件
        InputStream is = Resources.getResourceAsStream("mybatis.xml");
        //2.创建SqlSessionFactoryBuilder对象
        SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
        //3.创建SqlSessionFactory对象
        SqlSessionFactory factory = builder.build(is);
        //4.创建SqlSession对象
        SqlSession sqlSession = factory.openSession();
        //5.【重要】指定要执行的sql语句的标识。sql映射文件中的namespace + "." + 标签的id值
        String sqlId = "dao.StudentDao.queryStudents";
        //6.【重要】执行SQL语句，通过sqlId找到sql语句
        List<Student> students = sqlSession.selectList(sqlId);
        //7.遍历结果
        for (Student student : students) {
            System.out.println(student);
        }
        //8.关闭SqlSession对象
        sqlSession.close();
    }
}
